<template>
  <div class="loginContainer">
    <el-form ref="loginFormRef" :model="form" :rules="formRules" label-width="0" class="loginForm">
      <h2 style="text-align: center">登录</h2>
      <el-form-item prop="username">
        <el-input v-model="form.username" prefix-icon="el-icon-s-custom"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" prefix-icon="el-icon-edit" type="password"></el-input>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      formRules: {
        username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    reset() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async (validate) => {
        if (!validate) return
        const { data: res } = await this.$http.post('login', this.form)
        if (res.meta.status !== 200) return this.$message.error('登陆失败')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.loginContainer {
  background: url('https://images.pexels.com/photos/12368748/pexels-photo-12368748.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1')
    fixed no-repeat;
  background-size: cover;
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  .loginForm {
    width: 500px;
    height: 300px;
    padding: 40px;
    display: flex;
    flex-direction: column;
    position: relative;
    z-index: 100;
    background: inherit;
    border-radius: 8px;
    overflow: hidden;
    justify-content: space-between;

    &::before {
      content: '';
      width: calc(100% + 20px);
      height: calc(100% + 20px);
      position: absolute;
      top: -10px;
      left: -10px;
      overflow: hidden;
      background: inherit;
      filter: blur(5px);
      z-index: -1;
      box-shadow: inset 0 0 0 300px rgba(255, 255, 255, 0.25);
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
